﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CroslandSAP.Proxy;
using System.Data;
using CROSLAND.BL.BE;

namespace CROSLAND.DL.DALC.SAPC
{
    public class MaterialDALC : IMaterialDALC
    {
        
       public static MaterialDALC Instance() {
            if (_instance == null)
            {
                lock (typeof(MaterialDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new MaterialDALC();
                    }
                }
            }
            return _instance;
        }

        private MaterialDALC() { }
        private static volatile MaterialDALC _instance = null;


        #region IMaterialDALC Members


        public List<MaterialBE> buscarMateriales(string pCodMat, string pDesMat, string pJerMat, string pOrgVtas, string pCanalDist, string ofVtas, string usuarioWeb, string sector, string centro, string almacen)
        {
            MaterialesProxy mProxy = new MaterialesProxy();
            mProxy.Connection = DALC.getConnection;
            if (!mProxy.Connection.IsOpen) { mProxy.Connection.Open(); }
            /* llamando a la función */
            string ex_result = "";
            ZSSD_MATERIALESTable tblMateriales = new ZSSD_MATERIALESTable();
            mProxy.Zvmc_Sd_003("", almacen, pDesMat, pCodMat, pJerMat, sector, usuarioWeb, ofVtas, pOrgVtas, pCanalDist, centro, out ex_result, ref tblMateriales);

            SAP.Connector.SAPConnectionPool.ReturnConnection(mProxy.Connection);
            mProxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblMateriales.ToADODataTable();
            List<MaterialBE> lsMaterial = new List<MaterialBE>();
            foreach (DataRow dr in dt.Rows)
            {
                MaterialBE objMaterial = new MaterialBE();
                objMaterial.CodMaterial = dr["MATNR"].ToString();
                objMaterial.DescMaterial = dr["MAKTX"].ToString();
                objMaterial.CodUMedida = dr["MEINS"].ToString();
                objMaterial.PrecioUnit = Convert.ToDecimal(dr["NETPR"].ToString());
                objMaterial.Plista = Convert.ToDecimal(dr["NETPR"].ToString());
                objMaterial.Ppromo = Convert.ToDecimal(dr["PRPROM"].ToString());
                objMaterial.marca = dr["MARCA"].ToString();
                objMaterial.modelo = dr["MODELO"].ToString();
                objMaterial.moneda = dr["MONEDA"].ToString();
                objMaterial.Stock = Convert.ToDecimal(dr["STOCK"].ToString());
                //objMaterial.Jerarquia = dr["PRDHA"].ToString();
                //objMaterial.Peso = Convert.ToDecimal(dr["NTGEW"].ToString());
                //objMaterial.ValorRendimiento = Convert.ToDecimal(dr["UMREZ"].ToString());
                //objMaterial.UMedidaRendimiendo = dr["MEINH"].ToString();
                //objMaterial.TipoMaterial = dr["MFRGR"].ToString();
                //objMaterial.Tipo = dr["MTART"].ToString();
                //objMaterial.TieneServ = dr["EXTWG"].ToString().ToUpper() == "SI" ? true : false;
                //objMaterial.Descontinuado = dr["DESCONT"].ToString().ToUpper() == "X" ? "D" : "";
                //objMaterial.EspecialServ = dr["GROES"].ToString().ToUpper() == "E" ? true : false;
                //objMaterial.EsFlete = dr["KTGRM"].ToString().ToUpper().Equals("06") ? true : false;
                //objMaterial.EsEstiba = dr["KTGRM"].ToString().ToUpper().Equals("07") ? true : false;
                lsMaterial.Add(objMaterial);
            }
            return lsMaterial;
        }

        #endregion

        # region Caracteristicas Material

        public List<GenericBE> caracteristicaMat(string pCodMat, string pLote)
        {
            MaterialesProxy proxy = new MaterialesProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            BAPICUVALTable tblCaract = new BAPICUVALTable();
            proxy.Zvmc_Sd_036(pLote, pCodMat, ref tblCaract);
            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            List<GenericBE> lstMat = new List<GenericBE>();
            GenericBE objGenBE = null;
            foreach (BAPICUVAL itm in tblCaract)
            {
                objGenBE = new GenericBE();
                objGenBE.Codigo = itm.Charc;
                objGenBE.Descripcion = itm.Value_Txt;
                lstMat.Add(objGenBE);
                //CaracteristicaBE objMaterial = new CaracteristicaBE();
                //int idx = tblCaract.FindBy("CHAR_C", "ZCOLOR");
                //objMaterial.color = tblCaract[idx].Value_Txt;
                ////objMaterial.altura = 
                ////lstMat.Add(objMaterial);
            }
            return lstMat;

            //GenericBE obj = lstMat.Find(X => X.Codigo == "ZZCOLOR");
            //txtalgo.Text = obj.Descripcion;
        }

        #endregion

        #region Materiales Reemplazo

        public List<MaterialBE> buscarMaterialesReemplazo(string pCodMat)
        {
            MaterialesProxy proxy = new MaterialesProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }

            ZSSD_MATERIALESTable tblMat = new ZSSD_MATERIALESTable();

            proxy.Zvmc_Sd_024(pCodMat, ref tblMat);
            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblMat.ToADODataTable();

            List<MaterialBE> lstMat = new List<MaterialBE>();
            foreach (DataRow dr in dt.Rows)
            {
                MaterialBE objMaterial = new MaterialBE();
                objMaterial.CodMaterial = dr["MATNR"].ToString();
                objMaterial.DescMaterial = dr["MAKTX"].ToString();
                objMaterial.CodUMedida = dr["MEINS"].ToString();
                objMaterial.PrecioUnit = Convert.ToDecimal(dr["NETPR"].ToString());
                objMaterial.Plista = Convert.ToDecimal(dr["NETPR"].ToString());
                objMaterial.Ppromo = Convert.ToDecimal(dr["PRPROM"].ToString());
                objMaterial.marca = dr["MARCA"].ToString();
                objMaterial.modelo = dr["MODELO"].ToString();
                objMaterial.Stock = Convert.ToDecimal(dr["STOCK"].ToString());
                lstMat.Add(objMaterial);
            }
            return lstMat;
        }

        #endregion

    }
}
